package cn.ad.server.common.utils;

import java.security.MessageDigest;

public class MD5Utils {

	private static String encodePassword(String password, String algorithm) {
		byte unencodedPassword[] = password.getBytes();
		MessageDigest md = null;
		try {
			md = MessageDigest.getInstance(algorithm);
		} catch (Exception e) {
			return password;
		}
		md.reset();
		md.update(unencodedPassword);
		byte encodedPassword[] = md.digest();
		StringBuffer buf = new StringBuffer();
		for (int i = 0; i < encodedPassword.length; i++) {
			if ((encodedPassword[i] & 255) < 16)
				buf.append("0");
			buf.append(Long.toString(encodedPassword[i] & 255, 16));
		}

		return buf.toString();
	}

	public static final String getMD5(String passWord) {
		return encodePassword(passWord, "MD5");
	}

	public static final String getMD5(String mobile, String passWord) {
		return encodePassword((new StringBuilder(String.valueOf(mobile))).append(passWord).toString(), "MD5");
	}
}
